
clear all 
set more off 
set maxvar 15000 
clear matrix

**********************************************************************************************************************************************
**********************************************************************************************************************************************
**** IGE VS RANK OVER TIME -- VARYING AGE OF SAMPLE
**********************************************************************************************************************************************
**********************************************************************************************************************************************
    
	use "$Mydirectory1/3_Output/2_PooledData_analysis.dta", clear 
    keep if baseline_sample==1
    
* Select sample
    gen sample = eduR!=. & age!=. & log_father_baseline!=. & log_son_baseline!=. 
    keep if sample==1
    
* Make controls
    gen age_minus40_1 = age-40
    gen age_minus40_2 = age_minus40_1*age_minus40_1
    gen age_minus40_3 = age_minus40_1*age_minus40_1*age_minus40_1
    gen age_minus40_4 = age_minus40_1*age_minus40_1*age_minus40_1*age_minus40_1
    
* Education x age interactions
    tab eduR, gen(eduR_)
    forval i=1(1)7 {
        gen interaction_edu`i'_age = eduR_`i' * age
        gen interaction_edu`i'_agesq = eduR_`i' * agesq
    }

***************************************
**** NOW RUN LOOP FOR IGE OVER TIME ****
***************************************

    foreach var in rank ige  {
    preserve
    estimates clear 
        
    * Dependent variables
        if "`var'"=="ige" local dep "log_son_baseline"
        if "`var'"=="rank" local dep "rank_son_baseline"
        
    * Independent variables
        if "`var'"=="ige" local indep "log_father_baseline"
        if "`var'"=="rank" local indep "rank_father_baseline"
        
    * Variables needed 
        forval i=1(1)4 {
            gen interaction_inc_`i' = `indep'*age_minus40_`i'
        }
        
    * Save estimates
        forval i=1(1)7 {
            gen est_`i'=. 
            gen est_lb_`i' =.
            gen est_ub_`i' =.
        }
        
        foreach fix in 1 2 3 4 5 6 7  {

        * Control vars or conditions
            if "`fix'"=="1" local cond " "
            if "`fix'"=="2" local cond " "
            if "`fix'"=="3" local cond "age agesq"			
            if "`fix'"=="4" local cond "age_minus40*"
            if "`fix'"=="5" local cond "age_minus40* interaction_inc_*"
            if "`fix'"=="6" local cond "coll_ed age agesq" 
            if "`fix'"=="7" local cond "interaction_edu* age agesq" 
            
            if "`fix'"=="2" local cond0 "& age>=35 & age<=50"
            if "`fix'"!="2" local cond0 " "
            
            levelsof decade, local(decades)
            
            foreach x of local decades {
            eststo : quietly reg `dep' `indep' `cond' if decade==`x' `cond0' [pw=wgt_sex_race], robust 
                replace est_`fix' = _b[`indep'] if decade==`x'
                replace est_ub_`fix' = _b[`indep']+1.96*_se[`indep'] if decade==`x'
                replace est_lb_`fix' = _b[`indep']-1.96*_se[`indep'] if decade==`x'
            }

        }
            
        * Now graph
            bysort decade: keep if _n==1
            keep decade est_*
            
            reshape long est_ est_lb_ est_ub_, i(decade) j(estimate)
            replace decade= decade+1 if estimate==2 
            replace decade= decade+2 if estimate==3
            replace decade= decade+3 if estimate==4
            replace decade= decade+4 if estimate==5
            replace decade= decade+5 if estimate==6
            replace decade= decade+6 if estimate==7			
            
            if "`var'"=="ige" {
                local title "IGE"
                local ub "1"
                local lb "0"
                local skip "0.25"
                local maincolor "midblue"
                local color2 "navy"
                local color3 "eltblue"
                local color4 "teal"
                local color5 "green"
                local color6 "mint*1.2"
                local color7 "orange*0.5"				
                local m "8"
                local ub2 "0.5"
                local lb2 "0"
                local skip2 "0.1"
            }

            if "`var'"=="rank" {
                local title "Rank"
                local ub "0.45"
                local lb "0"
                local skip "0.15"
                local maincolor "orange"
                local color2 "cranberry"
                local color3 "pink*0.75"
                local color4 "erose"
                local color5 "lavender"
                local color6 "purple"
                local color7 "gray"				
                local m "8"
                local ub2 "0.3"
                local lb2 "0"
                local skip2 "0.1"
            }
            
            #delimit ;
            twoway (scatter est_ decade if estimate==1,  msymbol(circle) mcolor(`maincolor') msize(medium) ) 
                   (rcap est_lb_  est_ub_  decade if estimate==1, lpatter(solid) lcolor(`maincolor') lwidth(0.5))
                    (scatter est_ decade if estimate==2,  msymbol(diamond) mcolor(`color2') msize(small) ) 
                   (rcap est_lb_  est_ub_  decade if estimate==2, lpatter(solid) lcolor(`color2') lwidth(0.5) )
                    (scatter est_ decade if estimate==3,  msymbol(triangle) mcolor(`color3') msize(small) ) 
                   (rcap est_lb_  est_ub_  decade if estimate==3, lpatter(solid) lcolor(`color3') lwidth(0.5) )
                    (scatter est_ decade if estimate==4,  msymbol(square) mcolor(`color4') msize(small) yaxis(1)) 
                   (rcap est_lb_  est_ub_  decade if estimate==4, lpatter(solid) lcolor(`color4') lwidth(0.5) yaxis(1)) 
                   (scatter est_ decade if estimate==5,  msymbol(square_hollow) mcolor(`color5') msize(small) yaxis(1) ) 
                   (rcap est_lb_  est_ub_  decade if estimate==5, lpatter(solid) lcolor(`color5') lwidth(0.5) yaxis(1)) 
					(scatter est_ decade if estimate==6,  msymbol(triangle_hollow) mcolor(`color6') msize(small) yaxis(1) ) 
                   (rcap est_lb_  est_ub_  decade if estimate==6, lpatter(solid) lcolor(`color6') lwidth(0.5) yaxis(1)) 				   
                   (scatter est_ decade if estimate==7,  msymbol(diamond_hollow) mcolor(`color7') msize(small) yaxis(2) ) 
                   (rcap est_lb_  est_ub_  decade if estimate==7, lpatter(solid) lcolor(`color7') lwidth(0.5) yaxis(2)) 
                   ,
            xti(" " "Decade of respondent's birth") xlabel(1910(10)1970) xscale(range(1905 1975))
            ylabel(`lb'(`skip')`ub', axis(1)) ylabel(`lb2'(`skip2')`ub2', axis(2))
            yti("`title' coefficient (except age x edu.)" " ", axis(1)) yti(" " "`title' coefficient (age x edu.)", axis(2)) 
            legend(on ring(0) pos(`m') rows(2) order(1 "Baseline" 3 "Ages 35-50" 5 "Age quadratic " 7 "Age quartic" 9 "Age x income" 11 "College ed." 13 "Age x edu."))
            xlabel(1910 "1910s" 1920 "1920s" 1930 "1930s" 1940 "1940s" 1950 "1950s" 1960 "1960s" 1970 "1970s", labsize(small) ) ;  
            #delimit cr
            graph export "$Mydirectory2/appendix_b/`title'_overtime_lifecyclebias.pdf", as(pdf) replace

                
        restore

    }

